package com.dzjcloud.dzj.common.core.utils;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import lombok.experimental.UtilityClass;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <h3>bangbang</h3>
 * <p>http请求工具类</p>
 *
 * @author : DongZeJian
 * @date : 2020-12-31 16:12
 **/
@UtilityClass
public class HttpUtils {

	/**
	 * 获取HttpServletRequest对象
	 *
	 * @return
	 */
	public HttpServletRequest getHttpServletRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	/**
	 * 获取HttpServletResponse对象
	 *
	 * @return
	 */
	public HttpServletResponse getHttpServletResponse() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
	}


	/**
	 * 输出信息到浏览器
	 *
	 * @param response : 响应结果
	 * @param data     : 输出响应数据
	 * @throws IOException
	 */
	public void writeSuccess(HttpServletResponse response, Object data) throws IOException {
		response.setContentType("application/json;charset=UTF-8");
		R httpR = HttpRUtils.success(data);
		JSON resultJSONString = JSONUtil.parse(httpR);
		response.getWriter().print(resultJSONString);
		response.getWriter().flush();
		response.getWriter().close();
	}

	/**
	 * 输出信息到浏览器
	 *
	 * @param response : 响应结果
	 * @param errorMsg : 输出响应数据
	 * @throws IOException
	 */
	public static void writeError(HttpServletResponse response, String errorMsg) throws IOException {
		response.setContentType("application/json;charset=UTF-8");
		R httpR = HttpRUtils.error(errorMsg);
		JSON resultJSONString = JSONUtil.parse(httpR);
		response.getWriter().print(resultJSONString);
		response.getWriter().flush();
		response.getWriter().close();
	}
}
